package com.apps.ftapp.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.apps.ftapp.R;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.SharedPreferencesUtil;
import com.apps.ftapp.util.StatusBarUtils;
import com.jaeger.library.StatusBarUtil;

import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

/**
 * Created by Administrator on 2017/4/9.
 */

public abstract class BaseActivity extends AppCompatActivity {

    private LinearLayout rootLayout;
    protected int mColorId = R.color.colorWhite;//状态栏的默认背景色
    protected TextView commTitle;
    protected ImageView backBtn;
    protected ImageView shareBtn;

    protected TextView qingkong;
    protected TextView registBtn;

    private long firstTime = 0;

    protected TextView daojishi;
    protected RelativeLayout relativeLayout;
    protected ImageView fenxiang;
    protected ImageView timu;
    protected ImageView shoucang;

    protected TextView quanbu;

    protected TextView quanping;

    protected Toolbar toolbar;

    protected TextView save;

//
//    @Override
//    public boolean onKeyUp(int keyCode, KeyEvent event) {
//        switch (keyCode){
//            case KeyEvent.KEYCODE_BACK:
//                long secondTime=System.currentTimeMillis();
//                if(secondTime-firstTime>2000){
//                    Toasty.normal(getApplicationContext(),"再按一次退出程序").show();
//                    firstTime=secondTime;
////                    finish();
//                    return true;
//                }else{
//                    System.exit(0);
//                }
//                break;
//        }
//        return super.onKeyUp(keyCode, event);
//    }
//
//    // 定义一个变量，来标识是否退出
//    private static boolean isExit = false;
//
//    Handler mHandler = new Handler() {
//
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            isExit = false;
//        }
//    };
//
//
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            exit();
//            return false;
//        }
//        return super.onKeyDown(keyCode, event);
//    }
//
//    private void exit() {
//        if (!isExit) {
//            isExit = true;
//            Toasty.normal(getApplicationContext(),"再按一次退出程序").show();
////            Toast.makeText(getApplicationContext(), "再按一次退出程序",
////                    Toast.LENGTH_SHORT).show();
//            // 利用handler延迟发送更改状态信息
//            mHandler.sendEmptyMessageDelayed(0, 2000);
//        } else {
//            finish();
//            System.exit(0);
//        }
//    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        //  requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 这句很关键，注意是调用父类的方法
        super.setContentView(R.layout.activity_base);
        commTitle = findViewById(R.id.commTitle);
        backBtn = findViewById(R.id.backBtn);
        shareBtn = findViewById(R.id.shareBtn);
        registBtn = findViewById(R.id.regist_btn);
        qingkong = findViewById(R.id.qingkong);

        toolbar = findViewById(R.id.toolbar);

        quanbu = findViewById(R.id.quanbu);

        daojishi = findViewById(R.id.daojishi);
        relativeLayout = findViewById(R.id.gengduo);
        fenxiang = findViewById(R.id.fenxiang);
        timu = findViewById(R.id.timu);
        shoucang = findViewById(R.id.shoucang);

        quanping = findViewById(R.id.quanping);

        save = findViewById(R.id.save);

        //初始化状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }


        //读取缓存信息
        if (!Constant.IS_DEBUG) {
            Constant.uid = SharedPreferencesUtil.getData(getApplicationContext(), "uid", "") + "";
            Constant.token = SharedPreferencesUtil.getData(getApplicationContext(), "token", "") + "";
        }


        if (Constant.uid == null || "".equals(Constant.uid)) {

            //      Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
            //     startActivity(intent);
            //     finish();
        }

        Log.i("MAIN", Constant.token + "................");
        StatusBarUtil.setColor(BaseActivity.this, Color.WHITE, 1);
        StatusBarUtils.StatusBarLightMode(this);
        // StatusBarUtil.setTranslucent(BaseActivity.this);

        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }


    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    protected void setStatusBar() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.white));
    }


    public void setBack(int isShow) {
        backBtn.setVisibility(isShow);
    }


    public void setTitle(String title) {
        commTitle.setText(title);
    }


    /**
     * 如果子类使用非默认的StatusBar,就重写此方法,传入布局的id
     */
    protected void setColorId() {
        //this.mColorId=R.color.XXX;子类重写方式
    }

    protected int getColorId() {
        return mColorId;
    }


    /**
     * 子类是否需要实现沉浸式,默认需要
     *
     * @return
     */
    protected boolean isNeedLoadStatusBar() {
        return true;
    }

    /**
     * 初始化标题栏
     */
    private void initToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }
        getSupportActionBar().setDisplayShowTitleEnabled(false);

    }


    @Override
    public void setContentView(int layoutId) {
        setContentView(View.inflate(this, layoutId, null));
    }

    @Override
    public void setContentView(View view) {
        rootLayout = findViewById(R.id.root_layout);
        if (rootLayout == null) return;
        rootLayout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        initToolbar();
    }

}
